﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using BattleCore;
using BattleCore.Events;
using PsyModules;

namespace Battle
{
    // Add the attribute and the base class
    [Behavior("Help", "true", "1.00", "PsyOps", "Help Manager.")]
    public class BattleHelp:BotEventListener
    {
        public BattleHelp()
        {
            // Help for freq commands
            RegisterCommand("!help", do_help);
            // Help for freq commands
            RegisterCommand(".help", do_help);
        }

        //-------------------------------------\\
        //       BotID Info                     |
        //-------------------------------------//
        // Stores botinfo
        string BotName, Arena = "";

        ShortChat msg = new ShortChat();    // custom module

        public void do_help(ChatEvent e)
        {
            if (e.Message.ToLower() == "!help" || e.Message.ToLower() == ".help")
                e.Message = "!help overview";

            if (e.Message.Length > 5)
            {
                string data = e.Message.Substring(5, e.Message.Length - 5).Trim();
                string mainpath = "help/" + data.ToLower() + ".info";
                string filePath = BotName + "/" + mainpath;
                string line;
                bool ModCheckOn = false;
                string Modlvl = "None";
                bool deny = false;

                // Making sure it exists
                if (File.Exists(filePath))
                {
                    StreamReader file = null;
                    try
                    {
                        file = new StreamReader(filePath);
                        // Check all lines
                        while ((line = file.ReadLine()) != null)
                        {
                            if (line.StartsWith("[MOD]"))
                            {
                                string[] data0 = line.Split(']');
                                Modlvl = data0[1].Trim();
                                ModCheckOn = true;
                                Game(msg.pm(e.PlayerName, "[*] Staff Menu - ModLevel[" + Modlvl + "] [*]"));
                            }
                            else if (((ModCheckOn) && (ModCheck(e, Modlvl))) || (!ModCheckOn))
                                Game(msg.pm(e.PlayerName, line));
                            else
                                deny = true;
                        }
                    }
                    finally
                    {
                        if (file != null)
                            file.Close();
                    }
                }
                // Making sure it exists
                else if (File.Exists(mainpath))
                {
                    StreamReader file = null;
                    try
                    {
                        file = new StreamReader(mainpath);
                        // Check all lines
                        while ((line = file.ReadLine()) != null)
                        {
                            if (line.StartsWith("[MOD]"))
                            {
                                string[] data0 = line.Split(']');
                                Modlvl = data0[1].Trim();
                                ModCheckOn = true;
                                Game(msg.pm(e.PlayerName, "[*] Staff Menu - ModLevel[" + Modlvl + "] [*]"));
                            }
                            else if (((ModCheckOn) && (ModCheck(e, Modlvl))) || (!ModCheckOn))
                                Game(msg.pm(e.PlayerName, line));
                            else
                                deny = true;
                        }
                    }
                    finally
                    {
                        if (file != null)
                            file.Close();
                    }
                }
                else
                {
                    Game(msg.pm(e.PlayerName, "Im sorry i do not understand [" + data + "]"));
                }
                if (deny)
                    Game(msg.pm(e.PlayerName, "You are not authorized to view this menu. Sorry."));
            }
        }

        public Boolean ModCheck(ChatEvent c, string modlvl)
        {
            ModLevels Help_Mod_Lvl = new ModLevels();
            Help_Mod_Lvl = ModLevels.None;

            if (modlvl == "Sysop")
                Help_Mod_Lvl = ModLevels.Sysop;
            else if (modlvl == "Smod")
                Help_Mod_Lvl = ModLevels.SMod;
            else if (modlvl == "Mod")
                Help_Mod_Lvl = ModLevels.Mod;
            else
                Help_Mod_Lvl = ModLevels.None;

            if (c.ModLevel >= Help_Mod_Lvl)
                return true;
            else
                return false;
        }

        public void GetID(object sender, ChatEvent c)
        {
            // when core starts up it sends this message with bot info attached
            if (c.ChatType == ChatTypes.Private && c.Message.StartsWith("@BotInfo@"))
            {
                // Catch and store bot info that core sends
                string[] data = c.Message.Split(':');
                BotName = data[1];
                Arena = data[2];
            }
        }

        public override void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}
